package
{
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.getTimer;
	
	public class Test
	{
		/* Junk for testing. All of this will eventually vanish. */
		public static function initdebug():void {
			/******* Debug Hooks *******/
			m1.debug.addCommand("time", function():void{debug("time=" + getTimer())});
			//m1.debug.addCommand("fr", function():void{debug("framerate=" + stage.frameRate)});
			m1.debug.addCommand("pd", function():void{debug("period=" + m1.SysClk.getperiod())});
			m1.debug.addCommand("pc", function():void{debug("pc=" + m1.SysClk.pc)});
			/****/
			m1.debug.addCommand("mem", function():void{debug("Physical Bytes Used: " + RAM.bytes.length)});
			m1.debug.addCommand("cpu", function():void{CPU.dump()});
			m1.debug.addCommand("hlt", function():void{CPU.halt()});
			m1.debug.addCommand("init", function():void{CPU.reset()});
			/****/
			//debug.addCommand("condbg on", function():void{Console.dbg = true});
			//debug.addCommand("condbg off", function():void{Console.dbg = false});
			m1.debug.addCommand("irqdbg on", function():void{IRQ.dbg = true});
			m1.debug.addCommand("irqdbg off", function():void{IRQ.dbg = false});
			
			m1.debug.addCommand("disk sync", function():void{Disk.Sync()});
						
			m1.debug.addCommand("dformat", function():void{Disk.Format()});
			m1.debug.addCommand("vsave", function():void{vidsave()});
			m1.debug.addCommand("vload", function():void{vidrestore()});
			
			m1.debug.addCommand("vfill", function():void{vfill()});
			
			m1.debug.addCommand("vsync", function():void{Console.VideoSync()});
			m1.debug.addCommand("vtest", function():void{vtest()});
			/***************************/
		}
		
		/***************************/
		/* VRAM Testing */
		
		public static function vidsave():void {
			try {
				RAM.MemCopy(RAM.vRAMbottom, 10000, Console.TvH*4*10);
				Disk.WriteBlocks(10000, 0, Console.TvH*4*10/512);
				Disk.Sync();
			} catch (error:Error) {
				debug("vidsave error");
			}
		}
		
		public static function vidrestore():void {
			try {
				Disk.ReadBlocks(10000, 0, Console.TvH*4*10/512);
				RAM.MemCopy(10000, RAM.vRAMbottom, Console.TvH*4*10);
			} catch (error:Error) {
				debug("vidrestore error");
			}			
		}
		
		public static function vfill():void {
			try {
				for (var i:uint = 0; i<(Console.TvH*Console.TvY*4); i++) RAM.WriteW(RAM.vRAMbottom + i, 0x000000);
			} catch (error:Error) {
				debug("vfill error");
			}
			debug("vfill ok")
		}
		
		public static function vtest():void {
			try {
				//Console.TvRAM.setPixels(Console.TvRect, 
			} catch (error:Error) {
				debug("vtest error");
			}
			debug("vtest ok")
		}
		
		/***************************/
		
		/* testing only: very stupid char generator */
		public static function mkchar(ch:uint):ByteArray {
			var fcount:uint = RAM.bytes[0];
			//var fw:uint = RAM.bytes[1];
			var fh:uint = RAM.bytes[2];
			var char:ByteArray = new ByteArray();
			RAM.bytes.position = 2 + ch*fh;
			var i:uint, j:uint;
			var r:uint, c:uint;
			for (j=0; j<ch; j++) {
				for (i=8; i>0; i--) {
					RAM.bytes.position = 2 + ch*fh + j;
					r = RAM.bytes.readUnsignedByte();
					c = 1-((r>>i)&1);
					char.writeByte(255*c);
					char.writeByte(255*c);
					char.writeByte(255*c);
					char.writeByte(255*c);
				}
			}
			char.position = 0;
			return char;
		};
		
		public static function drawchar(x:uint, y:uint):void {
			debug("t="+Clock.optime(function():void{Console.TvRAM.setPixels(new Rectangle(0+x,0+y,8,16), Test.mkchar(90))}));
		}

		public static function setup():void {
			debug("Starting.");
			//RAM.bytes[1024*768*1] = 0;
			
			debug("FastMem Test");
			debug("fastmem_t="+Clock.optime(function():void{
				fastMemTest(); /* unopt=5587;  */
			}));
			
			debug("End of FastMem Test");
			
			if (Memory.Broken) debug("FastMem Doesn't Work!");
			
			
			debug("mempos=" + RAM.bytes.position);
			
			RAM.bytes.position = 0;
			
			try {
				RAM.WriteW(0x80000000, 0xFF0000);
				debug("read=" + RAM.ReadW(0x80000000));
				//debug.print("read=" + RAM.ReadW(100));
			} catch (error:Error) {
				debug("read error");
			}
			
			Disk.InitDisk();
			
			//var tst:ByteArray = Console.TvRAM.getPixels(new Rectangle(1,1,fw,fh));
			//debug("tst="+tst.length);
			
			/*
			debug("t="+Clock.optime(function():void{
				Console.TvRAM.setPixels(new Rectangle(1,1,1024,768), RAM.bytes)
			}));
			*/
			
			//debug("t="+Clock.optime(function():void{Console.TvRAM.floodFill(10,10,0x000000)}));
		}

		public static function fastMemTest():void {
			/* http://blog.joa-ebert.com/2009/08/05/turbodieselsportinjection/ */
			var _buffer:ByteArray = new ByteArray();
			_buffer.endian = Endian.LITTLE_ENDIAN;
			_buffer.length = 4096;
			Memory.select(_buffer);
			var i:uint, j:uint;
			for (j = 0; j < 1000; j++) {
				for (i = 0; i < 4096; i++) Memory.writeByte(255, i);
			}
		}

        private static function debug(msg:String):void {
        	m1.debug.print("TEST: " + msg);
        }
	}
}